<?php

class news extends table{

  public static  $CLASS_NAME  = 'news';
  public static  $TABLE_NAME  = 't_news';
  public static  $PRIMARY_KEY = 'news_id';
  
  var $news_id;
  var $news_type;
  var $news_title;
  var $news_txt;
  var $news_datedp;
  var $news_datefp;
  var $news_crdt;
  var $news_twitter;
  var $news_facebook;
  var $news_ontop;
  var $news_active;

  static function getTableName() {
    return self::$TABLE_NAME;
  }

  static function getClassName() {
    return self::$CLASS_NAME;
  }

  static function getPrimaryKey() {
    return self::$PRIMARY_KEY;
  }
 
  public function __construct($array) {
    
    $array = array_merge($array,array(
        'news_type'=>'1'
    ));
    
    parent::__construct($array);
    return $this;
  }
  
  public static function getActiveNews(){
    $sql = "select * from t_news where news_type=1 and news_datedp<=NOW() and news_datefp>=NOW() and news_active=1 order by news_ontop desc, news_datedp desc,news_datefp desc";
  
    $db = self::getDb();
    
    $db->Select($sql);
    
    $ret=array();
    
    while($row=$db->Next()){
      $ret[]=new news($row);
    }
    
    return($ret);
    
  }
  
  public static function getNews($id){
    $sql  = sprintf("select * from t_news where news_id=%d",$id);
    $db   = self::getDb();
    $ret  = $db->SelectFirst($sql);
    
    if($ret){
      $ret = new news($ret);
    }
    
    return $ret;
  }
  
  
  public static function getNewsLayout(){
    $news = self::getNews();
    
    $smarty=commun::getSmartyWidget();
    
    $smarty->assign_by_ref("newsTb", $news);
    
    
    $ret = $smarty->fetch('loliNews.'.TPL_EXT);
    
    return $ret;
  }
  
  public function delete(){
    $this->news_active=0;
    $this->update('news_active');
  }
  
  public function insert() {
    parent::insert(self::$CLASS_NAME);
  }
  
  public function update($fieldNames = '*') {
    parent::update($fieldNames, self::$CLASS_NAME);
  }
}

